package com.ziyueyuwen.xiniao.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.ziyueyuwen.xiniao.base.BaseUtil.*;
import static com.ziyueyuwen.xiniao.enums.ParamTypeEnum.INTEGER_GT_0;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author jun 2018/12/20.
 */
@Controller
@RequestMapping("/manage/order")
public class ManageOrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/list_order.do", method = RequestMethod.POST)
    public void listOrder(@RequestParam(PARAM_PAGE_NUM) Integer pageNum,
                          @RequestParam(PARAM_PAGE_SIZE) Integer pageSize,
                          @RequestParam(value = PARAM_PRODUCT_NAME, required = false) String productName,
                          @RequestParam(value = PARAM_USER_ACCOUNT, required = false) String userAccount,
                          @RequestParam(value = PARAM_USER_NAME, required = false) String userName,
                          HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取订单列表");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_PAGE_NUM, pageNum);
            checkParam(INTEGER_GT_0, PARAM_PAGE_SIZE, pageSize);
            // do.
            JSONObject retObj = orderService.listOrder(pageNum, pageSize, productName, userAccount, userName);
            saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/list_send.do", method = RequestMethod.POST)
    public void listSend(@RequestParam(PARAM_PAGE_NUM) Integer pageNum,
                         @RequestParam(PARAM_PAGE_SIZE) Integer pageSize,
                         @RequestParam(value = PARAM_PRODUCT_NAME, required = false) String productName,
                         @RequestParam(value = PARAM_USER_ACCOUNT, required = false) String userAccount,
                         @RequestParam(value = PARAM_USER_NAME, required = false) String userName,
                         HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<JSONObject> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取寄送订单列表");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_PAGE_NUM, pageNum);
            checkParam(INTEGER_GT_0, PARAM_PAGE_SIZE, pageSize);
            // do.
            JSONObject retObj = orderService.listSendOrder(pageNum, pageSize, productName, userAccount, userName);
            saveSuccessMsgAndData(hrc, retObj);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/remark_order.do", method = RequestMethod.POST)
    public void remarkOrder(@RequestParam(PARAM_ORDER_ID) Integer orderId,
                            @RequestParam(PARAM_REMARK) String remark,
                            HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("备注订单");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_ORDER_ID, orderId);
            checkParam(PARAM_REMARK, remark);
            // do.
            Boolean ret = orderService.remarkOrder(orderId, remark);
            saveSuccessMsgAndData(hrc, ret);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/send_book.do", method = RequestMethod.POST)
    public void sendBook(@RequestParam(PARAM_ORDER_ID) Integer orderId,
                         HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("标记已寄送");
        try {
            // check.
            checkParam(INTEGER_GT_0, PARAM_ORDER_ID, orderId);
            // do.
            Boolean ret = orderService.sendBook(orderId);
            saveSuccessMsgAndData(hrc, ret);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

}
